package jssia.database.mysql.columns;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;


public class YearColumn extends Column {

    private static final int LOWER_TWO_DIGIT_YEAR = 0;
    private static final int UPPER_TWO_DIGIT_YEAR = 99;
    private static final int LOWER_FOUR_DIGIT_YEAR = 1901;
    private static final int UPPER_FOUR_DIGIT_YEAR = 2069;

    public short get(ResultSet rs) throws SQLException {
        return rs.getShort(fullyQualifiedName);
    }

    public Date getDate(ResultSet rs) throws SQLException {
        return rs.getDate(fullyQualifiedName);
    }

    public void set(PreparedStatement ps, int index, int year) throws SQLException {
        if (year < LOWER_TWO_DIGIT_YEAR || (year > UPPER_TWO_DIGIT_YEAR && year < LOWER_FOUR_DIGIT_YEAR) || year > UPPER_FOUR_DIGIT_YEAR)
            throw new IllegalArgumentException("Invalid year of " + year + ", must be between " + LOWER_TWO_DIGIT_YEAR + " and " + UPPER_TWO_DIGIT_YEAR + " or between " + LOWER_FOUR_DIGIT_YEAR + " and " + UPPER_FOUR_DIGIT_YEAR);
        ps.setInt(index, year);
    }
}
